// pages/interviewer/interviewer.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    identityOptions: ['大学导师', '公司hr'],
    questionStyleOptions: ['追问式', '案例分析式', '假设情境式','技术白板讨论'],
    feedbackModeOptions: ['即时反馈', '总结式反馈', '延迟评估','自动化评分'],
    pressureLevelOptions: ['休闲对话', '职场模拟', '高压对抗'],
    languageOptions:['中文','英文'],
    professionalismOptions: ['1', '2', '3', '4', '5'],
    severityOptions: ['1', '2', '3', '4', '5'],
    patienceValueOptions: ['1', '2', '3', '4', '5'],
    encouragementOptions: ['1','2','3','4','5'],
    
    selectedIdentity: '',
    selectedQuestionStyle: '',
    selectedFeedbackMode: '',
    selectedPressureLevel: '',
    selectedLanguage:'',
    selectedProfessionalism: '',
    selectedSeverity: '',
    selectedEncouragementValue: '',
    selectedPatienceValue: ''
  },


  // 滑块值变化
  bindEncouragementChange(e) {
    this.setData({ 
      selectedEncouragementValue:this.data. encouragementOptions[e.detail.value]
    })
  },

  bindIdentityChange(e) {
    this.setData({
      selectedIdentity: this.data.identityOptions[e.detail.value]
    })
  },
  bindQuestionStyleChange(e) {
    this.setData({
      selectedQuestionStyle: this.data.questionStyleOptions[e.detail.value]
    })
  },
  bindFeedbackModeChange(e) {
    this.setData({
      selectedFeedbackMode: this.data.feedbackModeOptions[e.detail.value]
    })
  },
  bindPressureLevelChange(e) {
    this.setData({
      selectedPressureLevel: this.data.pressureLevelOptions[e.detail.value]
    })
  },
  bindProfessionalismChange(e) {
    this.setData({
      selectedProfessionalism: this.data.professionalismOptions[e.detail.value]
    })
  },
  bindSeverityChange(e) {
    this.setData({
      selectedSeverity: this.data.severityOptions[e.detail.value]
    })
  },
  bindPatienceValueChange(e) {
    this.setData({
      selectedPatienceValue: this.data.patienceValueOptions[e.detail.value]
    })
  },
  bindLanguageChange(e) {
    this.setData({
      selectedLanguage: this.data.languageOptions[e.detail.value]
    })
  },

  handleStartInterview() {
    // 必填字段校验配置
    const requiredFields = [
      { key: 'selectedIdentity', name: '身份' },
      { key: 'selectedQuestionStyle', name: '提问风格' },
      { key: 'selectedFeedbackMode', name: '反馈模式' },
      { key: 'selectedPressureLevel', name: '压力强度' },
      { key: 'selectedLanguage', name: '面试语言' },
      { key: 'selectedProfessionalism', name: '专业度' },
      { key: 'selectedSeverity', name: '严厉程度' },
      { key: 'encouragementValue', 
      name: '鼓励指数'},
      { key: 'selectedPatienceValue', name: '耐心值' }
    ]
    const userInfo = wx.getStorageSync('userInfo');
    const userId = userInfo.userid;
    // 收集未填项
    const missingFields = requiredFields
      .filter(({ key, name, validator }) => {
        const value = this.data[key]
        // 使用自定义校验规则或默认空值校验
        return (validator && validator(value)) || (!validator && value === '')
      })
      .map(item => item.name)

    // 存在未填项
    if (missingFields.length > 0) {
      wx.showToast({
        title: `请完成：${missingFields.join('、')}`,
        icon: 'none',
        duration: 3000
      })
      return
    }

    // 构建符合接口要求的参数
    const params = {
      "identity": this.data.selectedIdentity,
      "questioning style": this.data.selectedQuestionStyle,
      "feedback mode": this.data.selectedFeedbackMode,
      "pressure intensity": this.data.selectedPressureLevel,
      "professionalism index": parseInt(this.data.selectedProfessionalism),
      "strictness index": parseInt(this.data.selectedSeverity),
      "encouragement index": parseInt(this.data.selectedEncouragementValue),
      "patience index": parseInt(this.data.selectedPatienceValue),
      "language": this.data.selectedLanguage
    }
 
    // 提交到后端
    wx.request({
      url: 'https://pszap.top/interviewer/' + userId,
      method: 'POST',
      data: params,
      success: () => {
        wx.navigateTo({ url: '/pages/interview/interview' });
      },
      fail: () => {
        wx.showToast({ title: '提交失败', icon: 'none' });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.params) {
      // 解析回传参数
      const params = JSON.parse(options.params);
      this.setData({
        selectedIdentity: params.identity,
        selectedQuestionStyle: params["questioning style"],
        selectedFeedbackMode: params["feedback mode"],
        selectedPressureLevel: params["pressure intensity"],
        selectedProfessionalism: params["professionalism index"].toString(),
        selectedSeverity: params["strictness index"].toString(),
        encouragementValue: Math.round(params["encouragement index"] * 100),
        selectedPatienceValue: params["patience index"].toString(),
        selectedLanguage:params["language"]
      });
    }
  },

  // 其他生命周期函数保持为空即可...
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
})